﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml.Linq;
using System.IO;
using System.Xml;
using System.Text.RegularExpressions;
using HtmlAgilityPack;

namespace Parsing
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("lib.ru RSS parser");
                Console.WriteLine("[0] - файл, в котором будет сохранен исходный HTML");
                Console.WriteLine("[1] - файл со сгенерированным RSS");
                return;
            }
            HtmlWeb hweb = new HtmlWeb();
            XmlTextWriter xtr = new XmlTextWriter(new FileStream(args[0], FileMode.Create), Encoding.ASCII);
            hweb.LoadHtmlAsXml("http://lib.ru/lat/What-s-new", xtr);
            xtr.Flush();
            xtr.Close();
            HtmlDocument hd = new HtmlDocument();
            hd.Load(args[0]);
            List<Record> records = new List<Record>();
            foreach (HtmlNode node in hd.DocumentNode.ChildNodes)
            {
                if (node.Name == "pre")
                {
                    if (node.HasChildNodes)
                    {
                        string s = node.LastChild.InnerText;
                        if (s.Length > 31)
                        {
                            records.Add(new Record(s.Remove(0, 32)));
                            HtmlNode node2 = node;
                            while (node2.NextSibling.Elements("hr").Count() <= 0)
                            {
                                if (node2.NextSibling.Name == "ul")
                                {
                                    records[records.Count - 1].Author = node2.NextSibling.FirstChild.LastChild.InnerText;
                                    records[records.Count - 1].Link = "http://lib.ru/lat/" + node2.NextSibling.FirstChild.LastChild.Attributes["href"].Value;
                                    if (node2.NextSibling.NextSibling.Elements("b").Count() > 0)
                                        records[records.Count - 1].Caption = node2.NextSibling.NextSibling.Element("b").InnerText;
                                    else records[records.Count - 1].Caption = "";
                                }
                                else records.Add(new Record(s.Remove(0, 32)));
                                node2 = node2.NextSibling;
                            }
                        }
                    }
                }
            }
            XElement rss = new XElement("rss", new XAttribute("version", "2.0"));
            rss.Add(new XElement("channel"));
            foreach (Record record in records)
            {
                XElement item = new XElement("item");
                item.Add(new XElement("title", record.Author));
                item.Add(new XElement("link", record.Link));
                item.Add(new XElement("description", record.Caption));
                item.Add(new XElement("pubDate", record.Data));
                rss.Element("channel").Add(item);
            }
            rss.Save(args[1]);
        }
    }

    public class Record
    {
        public string Data;
        public string Author;
        public string Caption;
        public string Link;

        public Record(string data)
        {
            this.Data = data;
        }
    }
}
